package models

import "github.com/astaxie/goredis"

const (
	URL_QUEUE = "url_queue"
	URL_VISIT_SET = "url_visit_set"
)

var (
	client goredis.Client
)

func ConnectReids(addr string){
	client.Addr = addr
}

func GetQueueLength() int{
	length, err := client.Llen(URL_QUEUE)
	if err != nil {
		return 0
	}
	return length
}

func PutinQueue(url string){

	client.Lpush(URL_QUEUE, []byte(url))
}

func PopfromQueue() string{  //取出队列中的最后一项并从队列中将其删除
	res,err := client.Rpop(URL_QUEUE)
	if err != nil{
		panic(err)
	}

	return string(res)
}

func AddToSet(url string){
	 client.Sadd(URL_VISIT_SET, []byte(url))
}
func IsVisited(url string) bool{
	  bIsList,err :=client.Sismember(URL_VISIT_SET,[]byte(url))
	  if err != nil{
	  	return false
	  }

	  return bIsList
}
